class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int idx = 0;
        int n = nums.size();
        for (auto x : nums) {
            if (idx < 2 || nums[idx - 2] != x) {
                nums[idx++] = x;
            }
        }
        return idx;
    }
};